// pages/line/line.js
import * as echarts from '../../ec-canvas/echarts';

// 初始化图表函数  开始
let chart2 = null; 
function initChart2(canvas, width, height, dpr) {
  chart2 = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  })
  canvas.setChart(chart2)
  wx.getStorage({
    key:"data",
    success:function(res){
      var projectId = res.data[1]
      var projectSelectId = res.data[2]
      var projectSelectList = res.data[3]
      wx.request({
        url: 'http://localhost:8081/exam/getLineDate',
        data:{projectId:projectId,projectSelectId:projectSelectId},
        success:function(res){
          let date = dateDeal(res.data)
          wx.request({
            url: 'http://localhost:8081/exam/getLineData',
            data:{projectId:projectId,projectSelectId:projectSelectId},
            success:function(res2){
              let data = dataDeal(res2.data.data)
              wx.request({
                url: 'http://localhost:8081/exam/getLineName',
                data:{projectId:projectId,projectSelectId:projectSelectId},
                success:function(res3){
                  console.log(res3.data);
                  let option = getOption(date,data,res3.data,projectSelectList)
                  chart2.setOption(option)
                }
              })
            }
          })

        }
      })
      wx.request({
        url: 'url',
      })
    }
  })
  return chart2
}

function dateDeal(date){
  for(var i in date){
    date[i] = date[i].substring(date[i].length-4);
  }
  return date
}

function dataDeal(data){
  for(var i in data){
    data[i] = data[i].substring(0,data[i].length-1);
  }
  return data
}

function getOption(date,data,name,projectSelectList){

  return {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: projectSelectList
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: date
    },
    yAxis: {
      type: 'value',
      name:'百分比(%)',
      min:0,
      max:100
    },
    series: [
      {
        name: name,
        type: 'line',
        data: data
      }
    ]
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec2: {
      onInit: initChart2
    },
    date:[]
  },

  getDate:function(){
    var _this = this
    wx.getStorage({
      key:"data",
      success:function(res){
        var projectId = res.data[1]
        var projectSelectId = res.data[2]
        wx.request({
          url: 'http://localhost:8081/exam/getWrongListById',
          data:{projectId:projectId,projectSelectId:projectSelectId},
          success:function(res){
            _this.setData({
              date:res.data.data
            })
          }
        })
      }
    })
  },

  navigator:function(){
    wx.navigateTo({
      url: '/pages/lineDetail/lineDetail',
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})